package com.wontube.smr.remote.admin;

import com.wontube.core.annotation.NoLoginSupport;
import com.wontube.core.cache.Cacher;
import com.wontube.core.cache.EHCacheKey;
import com.wontube.core.pub.servlet.handlers.BasicHandler;
import com.wontube.smr.client.IDeviceHandler;
import com.wontube.smr.client.model.po.DeviceExpert;
import com.wontube.smr.dao.bo.DeviceExportDao;
import com.wontube.smr.web.client.admin.IAdminDeviceHandler;

/**
 * @author yuechao
 *
 */
@NoLoginSupport
public class AdminDeviceHandler extends BasicHandler implements IAdminDeviceHandler {
	@Override
	public boolean saveOrUpdateDevice(DeviceExpert device){
		boolean success = DeviceExportDao.saveOrUpdateDevice(device);
		if(success){
			deleteCaches();
		}			
		return success;
	}

	@Override
	public boolean deleteDevice(String pk) {
		boolean success = DeviceExportDao.deleteDevice(pk);
		if(success){
			deleteCaches();
		}			
		return success;
	}

	@Override
	public DeviceExpert searchDeviceByPK(String pk) {
		return DeviceExportDao.searchDeviceByPK(pk);
	}
	
	private void deleteCaches(){
		String clazzName = IDeviceHandler.class.getName();
		Cacher.defaultCacher.deleteCache(clazzName, "getAllSimulateAgent", new EHCacheKey(new Object[0]));
		
		Cacher.defaultCacher.deleteAllCache(clazzName, "getSimulateAgent", 2);
		
		Cacher.defaultCacher.deleteAllCache(clazzName, "isDownSupport", 2);
	}
}
